//
//  MemberDetialCell.swift
//  TinyShopKeeper
//
//  Created by huangtao on 16/6/1.
//  Copyright © 2016年 fish. All rights reserved.
//

import UIKit

let DefaultGrayColor = MZColor(226, green: 226, blue: 226)
class MemberDetialCell: UITableViewCell {

    //MARK: - Property
    var foodView:[FoodView] = [FoodView]()
    var numOfRows:Int = 0
    var index:Int = 0
    let classifyView:UIView
    let classifyLabel:UILabel
    let topTitleLabel:UILabel
    var colorType:ColorType = ColorType.Red
    var foodDatasource:[[String:AnyObject]]?{
        didSet{
            self .loadData()
        }
    }
    
    //MARK: -
  
    func loadData()
    {
        let type:Int = self.index % 2//关于红蓝的背景颜色~0,1
        self.colorType = ColorType(rawValue: type)!
        self.classifyLabel.backgroundColor = colorByType(self.colorType, style: ColorStyle.Deep)
        self.classifyView.backgroundColor = colorByType(self.colorType, style: ColorStyle.Deep)
        
        var startY:CGFloat = 0
        if self.index == 0{
            self.topTitleLabel.hidden = false//第一个的不隐藏
            startY += 35
            
        }else{
            self.topTitleLabel.hidden = true
        }
      
        let height = CGFloat((60+2) * self.foodDatasource!.count) -  CGFloat(2)//最后一个的2要去掉
        
        //
//        self.classifyView.frame = 
        self.classifyLabel.frame = CGRectMake(0, startY, 42, height)
        
        let startX:CGFloat = 45
        for singleFoodView in self.foodView{
            singleFoodView.removeFromSuperview()
        }
        self.foodView.removeAll()
        
        for food in self.foodDatasource!{
            //从xib文件中加载数据
            let singleFoodView = NSBundle.mainBundle().loadNibNamed("FoodView", owner: nil, options: nil).first as! FoodView//是数组取第一个
            singleFoodView.frame = CGRectMake(startX, startY, ScreenWidth*0.96 - startX, 60)
            singleFoodView.dictionary = food
            singleFoodView.leftView.backgroundColor = colorByType(self.colorType, style: ColorStyle.Shallow)
            
            self.contentView .addSubview(singleFoodView)
            self.foodView .append(singleFoodView)
            startY += 62
            
            
            //赋值
        }
    }
    
    //MARK: - LifeCycle
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        self.classifyLabel = UILabel(frame: CGRectMake(0,25,42,20))
        self.classifyLabel.textColor = UIColor.whiteColor()
        self.classifyLabel.numberOfLines = 0
        self.classifyLabel.text = "类型"
        self.classifyLabel.font = UIFont.systemFontOfSize(18)
        self.classifyLabel.textAlignment = .Center
        self.classifyView = UIView(frame: CGRectMake(0,25,ScreenWidth*42,20))
            
        self.topTitleLabel = UILabel(frame: CGRectMake(5,5,ScreenWidth*0.96,20))
        self.topTitleLabel.backgroundColor = DefaultGrayColor
        self.topTitleLabel.textColor = ApplicationTintColor
        self.topTitleLabel.text = "消费详情界面"
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
       
        self.contentView .addSubview(self.classifyView)
        self.contentView .addSubview(self.topTitleLabel)
        self.contentView .addSubview(self.classifyLabel)
        self.contentView .backgroundColor = DefaultGrayColor
        
        self.selectionStyle = .None
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   
    
}
